﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StatisticsApplication.WindowsForm
{
    public partial class TbControl : UserControl
    {
        private bool myStatus = false;

        /// <summary>
        /// 表示当前数据 有么有没有准备好，如果准备好了的话就为true 没有准备好的话就false   
        /// </summary>
        public bool MyStatus
        {
            get
            {
                return myStatus;
            }
            set
            {
                myStatus = value;
            }
        }
        /// <summary>
        /// 组数
        /// </summary>
        private int groupNum;

        public int GroupNum
        {
            get
            {
                return groupNum;
            }
            set
            {
                groupNum = value;
            }
        }
        /// <summary>
        /// 每组的个数。
        /// </summary>
        private int[] eachNum;

        public int[] EachNum
        {
            get { return eachNum; }
        }

        /// <summary>
        /// 输入数据的组合
        /// </summary>
        private string words;

        public string Words
        {
            get { return words; }
        }


        private string[] ownstr = new string[10];

        public TbControl()
        {
            InitializeComponent();
            SetTbControl(2);
            eachNum = new int[10];
        }
        public void SetTbControl(int count)
        {
            Enabledall();

            switch (count)
            {
                case 0:
                    textBox1.Enabled = false;
                    label1.Enabled = false;

                    textBox10.Enabled = false;
                    // textBox5.Text = string.Empty;
                    label10.Enabled = false;
                    goto case 1;

                case 1:
                    textBox2.Enabled = false;
                    label2.Enabled = false;

                    textBox9.Enabled = false;
                    // textBox5.Text = string.Empty;
                    label9.Enabled = false;
                    goto case 2;

                case 2:
                    textBox3.Enabled = false;
                    label3.Enabled = false;

                    textBox8.Enabled = false;
                    // textBox5.Text = string.Empty;
                    label8.Enabled = false;
                    goto case 3;
                case 3:
                    textBox4.Enabled = false;
                    label4.Enabled = false;

                    textBox7.Enabled = false;
                    // textBox4.Text = string.Empty;
                    label7.Enabled = false;
                    goto case 4;
                case 4:
                    textBox5.Enabled = false;
                    label5.Enabled = false;

                    textBox6.Enabled = false;
                    // textBox3.Text = string.Empty;
                    label6.Enabled = false;
                    goto case 5;
                case 5:

                    textBox17.Enabled = false;
                    label17.Enabled = false;

                    textBox16.Enabled = false;
                    label16.Enabled = false;
                    goto case 6;

                case 6:
                    textBox18.Enabled = false;
                    label18.Enabled = false;

                    textBox15.Enabled = false;
                    label15.Enabled = false;
                    goto case 7;

                case 7:
                    textBox19.Enabled = false;
                    label19.Enabled = false;

                    textBox14.Enabled = false;
                    label14.Enabled = false;
                    goto case 8;

                case 8:
                    textBox20.Enabled = false;
                    label20.Enabled = false;

                    textBox13.Enabled = false;
                    label13.Enabled = false;
                    goto case 9;

                case 9:
                    textBox21.Enabled = false;
                    label21.Enabled = false;

                    textBox12.Enabled = false;
                    label12.Enabled = false;
                    goto case 10;

                case 10:
                    break;
            }

        }
        public void Enabledall()
        {
            textBox1.Enabled = true;
            // textBox3.Text = string.Empty;
            label1.Enabled = true;

            textBox2.Enabled = true;
            // textBox3.Text = string.Empty;
            label2.Enabled = true;

            textBox3.Enabled = true;
            // textBox3.Text = string.Empty;
            label3.Enabled = true;

            textBox4.Enabled = true;
            // textBox4.Text = string.Empty;
            label4.Enabled = true;

            textBox5.Enabled = true;
            // textBox5.Text = string.Empty;
            label5.Enabled = true;


            textBox6.Enabled = true;
            // textBox3.Text = string.Empty;
            label6.Enabled = true;

            textBox7.Enabled = true;
            // textBox4.Text = string.Empty;
            label7.Enabled = true;

            textBox8.Enabled = true;
            // textBox5.Text = string.Empty;
            label8.Enabled = true;


            textBox9.Enabled = true;
            // textBox3.Text = string.Empty;
            label9.Enabled = true;

            textBox10.Enabled = true;
            // textBox4.Text = string.Empty;
            label10.Enabled = true;

            textBox11.Enabled = true;
            // textBox5.Text = string.Empty;
            label11.Enabled = true;




            textBox17.Enabled = true;
            label17.Enabled = true;

            textBox16.Enabled = true;
            label16.Enabled = true;



            textBox18.Enabled = true;
            label18.Enabled = true;

            textBox15.Enabled = true;
            label15.Enabled = true;



            textBox19.Enabled = true;
            label19.Enabled = true;

            textBox14.Enabled = true;
            label14.Enabled = true;



            textBox20.Enabled = true;
            label20.Enabled = true;

            textBox13.Enabled = true;
            label13.Enabled = true;



            textBox21.Enabled = true;
            label21.Enabled = true;

            textBox12.Enabled = true;
            label12.Enabled = true;

        }

        #region 输入总组数

        private void textBox11_Validating(object sender, CancelEventArgs e)
        {

            if (!JudgeGroupnum())
            {
                //MessageBox.Show(null, "输入组数不合法！必须输入大于1小于5！", "温馨提示：", MessageBoxButtons.OK);
                errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                errorProvider1.SetError((Control)sender, "输入组数不合法！必须输入大于1小于10的数字！");
                e.Cancel = true;
                return;
            }
            else
            {
                //SetControl(num);
                // errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                errorProvider1.SetError((Control)sender, "");
                // e.Cancel = true;
                SetTbControl(groupNum);
                return;
            }
        }
        #endregion

        #region 输入每组的元素用空格分开

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (((Control)sender).Equals(textBox1))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox10.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[0] = textBox1.Text.Trim().ToString();
                }
                else if (((Control)sender).Equals(textBox16))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox17.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[5] = textBox16.Text.Trim().ToString();
                }

                errorProvider1.SetError((Control)sender, "");

            }
            catch (Exception ex)
            {
                //MessageBox.Show(null, "A组输入个数与限制输入不合法。请确认！", "温馨提示：", MessageBoxButtons.OK);
                errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                //e.Cancel = true;
            }
        }

        private void textBox2_Validating(object sender, CancelEventArgs e)
        {

            try
            {

                if (((Control)sender).Equals(textBox2))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox9.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[1] = textBox2.Text.Trim().ToString();
                }
                else if (((Control)sender).Equals(textBox15))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox18.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[6] = textBox15.Text.Trim().ToString();
                }

                errorProvider1.SetError((Control)sender, "");
            }
            catch (Exception ex)
            {
                errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                //e.Cancel = true;
                //MessageBox.Show(null, "B组输入个数与限制输入不合法。请确认！", "温馨提示：", MessageBoxButtons.OK);
            }

        }

        private void textBox3_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (((Control)sender).Equals(textBox3))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox8.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[2] = textBox3.Text.Trim().ToString();
                }
                else if (((Control)sender).Equals(textBox14))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox19.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[7] = textBox14.Text.Trim().ToString();
                }

                errorProvider1.SetError((Control)sender, "");
            }
            catch (Exception ex)
            {
                errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                //e.Cancel = true;
                //MessageBox.Show(null, "C组输入个数与限制输入不合法。请确认！", "温馨提示：", MessageBoxButtons.OK);
            }
        }

        private void textBox4_Validating(object sender, CancelEventArgs e)
        {
            try
            {


                if (((Control)sender).Equals(textBox4))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox7.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[3] = textBox4.Text.Trim().ToString();
                }
                else if (((Control)sender).Equals(textBox13))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox20.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[8] = textBox13.Text.Trim().ToString();
                }

                errorProvider1.SetError((Control)sender, "");
            }
            catch (Exception ex)
            {
                errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                // e.Cancel = true;
                //MessageBox.Show(null, "D组输入个数与限制输入不合法。请确认！", "温馨提示：", MessageBoxButtons.OK);
            }
        }

        private void textBox5_Validating(object sender, CancelEventArgs e)
        {

            try
            {


                if (((Control)sender).Equals(textBox5))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox6.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[4] = textBox5.Text.Trim().ToString();
                }
                else if (((Control)sender).Equals(textBox12))
                {
                    string[] kk = ((Control)sender).Text.Trim().ToString().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    int temp = int.Parse(textBox21.Text.Trim().ToString());
                    if (kk.Length != temp)
                    {
                        //MessageBox.Show(null, "A组输入个数与限制输入不相符。请确认！", "温馨提示：", MessageBoxButtons.OK);
                        errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                        errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                        return;
                        //e.Cancel = true;
                    }
                    ownstr[9] = textBox12.Text.Trim().ToString();
                }

                errorProvider1.SetError((Control)sender, "");
            }
            catch (Exception ex)
            {
                errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                errorProvider1.SetError((Control)sender, "输入个数与限制输入不相符。请确认！");
                //e.Cancel = true;
                // MessageBox.Show(null, "E组输入个数与限制输入不合法。请确认！", "温馨提示：", MessageBoxButtons.OK);
            }
        }

        #endregion

        #region 输入每组元素的个数

        private void textBox10_Validating(object sender, CancelEventArgs e)
        {
            if (((Control)sender).Equals(textBox10))
            {
                if (JudgeEachnum((Control)sender, 1))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }
            else if (((Control)sender).Equals(textBox17))
            {
                if (JudgeEachnum((Control)sender, 6))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }

        }

        private void textBox9_Validating(object sender, CancelEventArgs e)
        {

            if (((Control)sender).Equals(textBox9))
            {
                if (JudgeEachnum((Control)sender, 2))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }
            else if (((Control)sender).Equals(textBox18))
            {
                if (JudgeEachnum((Control)sender, 7))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }

        }

        private void textBox8_Validating(object sender, CancelEventArgs e)
        {           

            if (((Control)sender).Equals(textBox8))
            {
                if (JudgeEachnum((Control)sender, 3))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }
            else if (((Control)sender).Equals(textBox19))
            {
                if (JudgeEachnum((Control)sender, 8))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }

        }

        private void textBox7_Validating(object sender, CancelEventArgs e)
        {
            if (((Control)sender).Equals(textBox7))
            {
                if (JudgeEachnum((Control)sender, 4))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }
            else if (((Control)sender).Equals(textBox20))
            {
                if (JudgeEachnum((Control)sender, 9))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }
        }

        private void textBox6_Validating(object sender, CancelEventArgs e)
        {
          
            if (((Control)sender).Equals(textBox6))
            {
                if (JudgeEachnum((Control)sender, 5))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }
            else if (((Control)sender).Equals(textBox21))
            {
                if (JudgeEachnum((Control)sender, 10))
                {
                    errorProvider1.SetError((Control)sender, "");
                }
                else
                {

                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    errorProvider1.SetError((Control)sender, "每组的个数不能超过20！");
                }
            }
        }

        #endregion


        private bool JudgeGroupnum()
        {
            int num;
            try
            {
                groupNum = 0;
                num = int.Parse(textBox11.Text.ToString());
                if (num > 10 || num < 2)
                {
                    return false;
                }
                else
                {
                    groupNum = num;
                    return true;

                }
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断每组的个数是不是超过限制范围
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private bool JudgeEachnum(Control sender, int count)
        {
            int num;
            try
            {
                groupNum = 0;
                num = int.Parse(((Control)sender).Text.ToString());
                if (num > 20 || num < 1)
                {
                    eachNum[count - 1] = 0;
                    return false;
                }
                else
                {
                    //groupNum = num;
                    eachNum[count - 1] = num;
                    return true;

                }
            }
            catch (System.Exception ex)
            {
                eachNum[count - 1] = 0;
                return false;
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            int num = 0;
            words = string.Empty;
            if (!JudgeGroupnum())
            {
                errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                string str = string.Empty;
                str = "没有输入总组数!";
                errorProvider1.SetError((Control)sender, str);
                return;
            }

            for (int i = 0; i < groupNum; i++)
            {
                if (eachNum[i] == 0)
                {
                    errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                    string str = string.Empty;
                    str = (i + 1).ToString() + "组,没有输入!";
                    errorProvider1.SetError((Control)sender, str);
                    return;
                }
                num += eachNum[i];
                if (string.IsNullOrEmpty(ownstr[i]))
                {
                    string str = string.Empty;
                    str = (i + 1).ToString() + "组,没有输入!";
                    errorProvider1.SetError((Control)sender, str);
                    return;
                }
                else
                {
                    words += " " + ownstr[i];
                }

                // words+=
            }
            if (num > 100 || num < 2)
            {
                errorProvider1.SetIconAlignment((Control)sender, ErrorIconAlignment.MiddleRight);
                errorProvider1.SetError((Control)sender, "整体个数不能超过100个，少于两个！");
                return;
            }
            else
            {
                errorProvider1.SetError((Control)sender, "");
                SetTbControl(0);
                myStatus = true;
            }
            button1.Enabled = false;

        }

        private void btCancle_Click(object sender, EventArgs e)
        {
            SetTbControl(groupNum);
            button1.Enabled = true;
            myStatus = false;
        }
    }
}
